﻿#pragma strict

private var mousePosition : Vector3;
public var object : GameObject;
private var menuMaquina : boolean;
public var texturaMover : Texture2D = null;
public var texturaUpgrade : Texture2D = null;
public var texturaVender : Texture2D = null;
public var menuRect : Rect;
public var menuSelecionado : int = -1;
private var seguirMouse : boolean;


function Start () {
    menuMaquina = false;
    seguirMouse = true;
}

function Update () {
    
    
    if(seguirMouse)
    {        
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
        object.transform.position.x = mousePosition.x;
        object.transform.position.y = mousePosition.y;
    }
}

function OnGUI()
{
    if(menuMaquina)
    {
     	var menuLista : String[] = ["Mover","Upgrade","Vender"];
		var menuTextura : Texture[] = [texturaMover,texturaUpgrade,texturaVender];
		menuSelecionado=-1;
		menuSelecionado = GUI.SelectionGrid(new Rect(mousePosition.x, mousePosition.y, 30, 60), menuSelecionado, menuTextura, 1);
		menuSelecionado = GUI.SelectionGrid(new Rect(mousePosition.x+30, mousePosition.y, 70, 60), menuSelecionado, menuLista, 1);
		
        if(menuSelecionado == 0)
        {
            Debug.Log("entrei");
            menuSelecionado = -1;
            menuMaquina = false;
            Seguir();
        }
        else if(menuSelecionado == 1)
        {
            menuSelecionado = -1;
            menuMaquina=false;            

        	
        }
        else if(menuSelecionado == 2)
        {
            menuSelecionado = -1;
        	GUIgeral.dinheiro+=100;
            GameObject.Destroy(object);
            menuMaquina=false;
		}
    }
}


function OnLeftClick()
{    
    Parar();    
}

function OnRightClick()
{   
    if(!Maquina.seguindo)
    {
        
        menuMaquina = true;
        mousePosition = Input.mousePosition;
        mousePosition.y = Screen.height - mousePosition.y;
    }
        
}	
    
function OnTriggerEnter2D (col : Collider2D)
{    
    Maquina.colide = true;
}

function OnTriggerExit2D(other: Collider2D) 
{
        
    Maquina.colide = false;
}

function OnTriggerStay2D(other : Collider2D)
{
    Maquina.colide = true;
}

function Seguir()
{
    Debug.Log("Seguir");
    object.collider2D.isTrigger = true;
    object.rigidbody2D.isKinematic = false;
    seguirMouse = true;
    Maquina.seguindo = true;
}

function Parar()
{
    Debug.Log("Parar");
    if(!Maquina.colide)
    {
        object.collider2D.isTrigger = false;
        object.rigidbody2D.isKinematic = true;
        Maquina.seguindo = false;
        seguirMouse = false;
    }
    
}